package com.lenovo.locationmanager;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @ProjectName: GpsAutoTest
 * @Package: com.lenovo.gpsautotest.LocationManager
 * @ClassName: FileUtils
 * @Description: FileUtils类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/6/13 10:12
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/6/13 10:12
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class FileUtils {
    private static final String TAG = FileUtils.class.getSimpleName();
    public static final String LOCATION = "LOCATION";
    public static final String SATELLITE = "SATELLITE";

    /**
     * 获取输出流
     * @param dirName
     * @param mFileType
     * @param mFileName
     * @return
     */
    public FileWriter getFileWriter(String dirName, String mFileType,
                                          String mFileName) {
        File path;
        File folder;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            //外部存储
            path = Environment.getExternalStoragePublicDirectory("GpsOutputs");
            Log.d(TAG, "getFileOutputStream: Output in External Storage!");
        } else {
            //内部存储
            path = Environment.getDataDirectory();
            Log.d(TAG, "getFileOutputStream: Output in Internal Storage!");
        }
        if (mFileType.equals(LOCATION)) {
            folder = new File(path + "/gps/" + dirName + "/location");
        } else if (mFileType.equals(SATELLITE)) {
            folder = new File(path + "/gps/" + dirName + "/satellite");
        } else {
            folder = new File(path + "/gps/" + dirName + "/" + mFileType);
        }
        //如果目录不存在，创建
        if (!(folder.isDirectory() && folder.exists())) {
            folder.mkdirs();
        }
        File file = new File(folder, mFileName);
        try {
            if (file != null) {
                return new FileWriter(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void writeTitle(String mFileType,FileWriter fileWriter){
        try {
            if(mFileType.equals(LOCATION)){
                fileWriter.write("[SessionNo],[RetryNo],[Location Time],[Start Time],[Stop Time],[TTFF]" +
                        ",[Longitude],[Latitude],[Altitude],[True Longitude],[True Latitude],[Distance]\r\n");
            }else if(mFileType.equals(SATELLITE)){
                fileWriter.write("[SessionNo],[RetryNo],[Prn],[Snr],[Elevation],[Azimuth],[Flags]\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
